package Main.Latex;

import Main.FileUtils.FileUtil;
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;

import javax.swing.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DefaultImageLatex extends Composite{
    private List<List<String>> fileList = new ArrayList<>();
    private int[] indexArrays;
    private int colNum ;
    private int rowNums = 6;
    private int fileIndex;
    private int maxFigureNums = 10;
    private String miniPageWidth = "0.1";
    private String dir;

    public DefaultImageLatex(String dir){
        this.dir = dir;
    }

    public void addFileList(List l){
        //排序一下
        Collections.sort(l);
        fileList.add(l);
    }

    public void setRowNums(int row){
        rowNums = row;
    }

    public void toLatexFile() throws IOException {
        File file = new File(dir+"\\"+fileIndex+".tex");
        BufferedWriter bufferWriter = FileUtil.getBufferWriter(file);

        bufferWriter.write(getLexString());

        bufferWriter.close();
        fileIndex++;

    }

    public void createFiles(){
        colNum = fileList.size();
        indexArrays = new int[colNum];

        //计算总共要创建的 figure 和 file 数量
        int needFigureNums = fileList.get(0).size() % rowNums == 0 ? fileList.get(0).size() / rowNums : fileList.get(0).size() / rowNums + 1;
        int needFileNums = needFigureNums%maxFigureNums == 0 ? needFigureNums / maxFigureNums : needFigureNums / maxFigureNums + 1;

        //计算一个文件中的 figure 数量，最多是 10 个 figure
        for (int i=0;i < needFileNums; i++){
            if((i+1)<needFileNums){
                createSigleFile(maxFigureNums);
            }else {
                createSigleFile(needFigureNums - ( i - 1 ) * maxFigureNums );
            }

            //产生latex文件
            try {
                toLatexFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //清理组件
            components.clear();
        }
    }

    public void createSigleFile(int figureNums){
        Document document = new Document();
        document.addLineToPre("\\setlength{\\tabcolsep}{1.2mm}\n");
        Header header = new Header();
        addChild(header);
        addChild(document);

        Figure figure = null;
        for (int i=0;i<figureNums;i++){
            figure = createFigure();
            document.addChild(figure);
        }

        figure.addLineToSuf("\\balance\n");
        figure.addLineToSuf("\\bibliographystyle{plain}\n");

    }

    public Figure createFigure(){
        Figure figure = new Figure();
        figure.addLineToPre("\\centering\n");

        Minipage minipage = createMinipage("");
        minipage.addLineToSuf("\\vfill\n");
        figure.addChild(minipage);
        return figure;
    }

    private Minipage createMinipage(String str) {
        Minipage minipage = new Minipage(str);

        for (int i = 0; i < colNum; i++){
            Subfloat subfloat =  createSubfloat(i);
            subfloat.addLineToSuf("\\hfill\n");
            minipage.addChild(subfloat);
        }

        return minipage;
    }

    private Subfloat createSubfloat(int num) {
        Subfloat subfloat = new Subfloat();
        Minipage minipage = new Minipage(miniPageWidth);
        minipage.addLineToPre("\\centering\n");

        //设置每一张图片的路径
        int imgNums = (fileList.get(num).size() - indexArrays[num]) >= rowNums ? rowNums : (fileList.get(num).size() - indexArrays[num]);

        for (int i=0;i<imgNums;i++){
//            System.out.println(fileList.get(num).size());
            minipage.addChild(new IncludeGraphics(fileList.get(num).get(indexArrays[num])));
            indexArrays[num]++;
        }

        subfloat.addChild(minipage);
        return subfloat;
    }

    public void setMaxFigureNums(int maxFigureNums) {
        this.maxFigureNums = maxFigureNums;
    }

    public void setMiniPageWidth(String miniPageWidth) {
        this.miniPageWidth = miniPageWidth;
    }

    public static void main(String[] args) {
//        DefaultImageLatex aDefault = new DefaultImageLatex();
//        aDefault.createSigleFile();
//        try {
//            aDefault.toLatexFile("a");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
